package com.ssm.ioc;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 读取配置 ConfigUtil
 */
public class PropertiesUtil {

    //放置配置项
    private enum WebProperties {
        FILE("frame.properties"),
        BASE_PACKAGE("frame.webapp.base_package"),
        JSP_PATH("frame.webapp.jsp_path");
        private String value;

        private WebProperties(String value) {
            this.value = value;
        }

        public String value() {
            return value;
        }
    }

    private static Properties p;

    static {
        p = load(WebProperties.FILE.value());
    }

    // 读取 file 中指定的配置
    public static Properties load(String file) {
        Properties p = new Properties();
        InputStream is = ClassUtil.getClassLoader().getResourceAsStream(file);
        try {
            p.load(is);
            is.close();
        } catch (IOException e) {
            System.err.println("PropertiesUtil:load properties failure");
        }
        return p;
    }

    public static String getBasePackage() {
        String value = "";
        String key = WebProperties.BASE_PACKAGE.value();
        if (p.containsKey(key)) {
            value = p.getProperty(key);
        }
        return value;
    }

    //获取有JSP 页面的包的路径
    public static String getJspPath() {
        String value = "";
        String key = WebProperties.JSP_PATH.value();
        if (p.containsKey(key)) {
            value = p.getProperty(key);
        }
        return value;
    }
}